<?php

namespace app\achievement\controller;

use app\common\model\User;
use think\db;

/**
 * @author yq
 * 系统管理员管理
 */
class Users extends Base {

    /**
     * 管理员列表
     * @return unknown
     */
    public function index() {
        //搜索条件
        $account = input("param.account");
        if (!empty($account)) {
            $where['a.account'] = $account;
        }
        $user = new User();
        //查询字段
        $field = 'a.account,a.avatar_img,a.loginnum,a.last_login_ip,a.last_login_time,a.name,a.id,a.status,b.title,a.is_admin';
        //查询条件
        $where['a.is_delete'] = 0;
        //$where['a.a_id'] = session($this->set['db_name'].'a_id');
        //主表
        $dbname = $this->set['db_name'];
        //分表
        $sub_table = $this->set['auth_group'];
        $data = $user->getInfoAll($field, $where, $dbname, $sub_table);

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     *  修改用户帐号状态
     */
    public function userEditStatus() {
        if (request()->isPost()) {
            $id = input("param.id");
            //实例化系统菜单栏model
            $user = new User();
            $where = ['id' => $id];
            $db_name = $this->set['db_name'];
            //查询菜单栏数据
            $menuOne = $user->getOne('status', $where, $db_name);
            if ($menuOne['status'] == 1) {
                $update = $user->updateUserOne($where, ['status' => 0], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已禁止']);
            } else if ($menuOne['status'] == 0) {
                $update = $user->updateUserOne($where, ['status' => 1], $db_name);
                return json(['code' => 1, 'data' => '', 'msg' => '已开启']);
            }
        }
    }

    /**
     *     添加管理员用户
     */
    public function addUser() {
        $user = new User();
        if (request()->isPost()) {
            $param = $this->request->post();
			unset($param['file']);
            //查询管理员帐号是否存在
            $userOne = $user->getOne('', ['account' => $param['account']], $this->set['db_name']);
            if (!empty($userOne)) {
                return json(['code' => 2, 'msg' => '该管理员帐号已被使用!', 'data' => '']);
            }
            if (empty($param['avatar_img'])) {
                return json(['code' => 2, 'msg' => '请上传用户头像', 'data' => '']);
            }
            if (empty($param['status'])) {
                $param['status'] = 0;
            }
            //生成登录密码
            $password = setPassword(trim($param['password']));
            $param['password'] = $password['password'];
            $param['strict'] = $password['strict'];
            $param['is_admin'] = 0;
            //$param['a_id'] = session($this->set['db_name'].'a_id');
            $res = $user->InsertUser($param, $this->set['db_name']);
            if ($res) {
                //添加用户组关联数据
                $group_access['uid'] = $res;
                $group_access['group_id'] = $param['groupid'];
                Db::name($this->set['auth_group_access'])->insertGetId($group_access);

                return json(['code' => 1, 'msg' => '添加管理员成功', 'data' => url('Authgroup/index') . '?type=2']);
            } else {
                return json(['code' => 2, 'msg' => '添加管理员失败', 'data' => '']);
            }
        }

        // 渲染添加管理员页面
        $auth_group = $user->getAllAuthGroup($this->set['auth_group'], ['id' => ['<>', 1], 'status' => 1]);
        $this->assign('data', $auth_group);
        return $this->fetch();
    }

    /**
     * 修改用户信息
     */
    public function updateUser() {
        $user = new User();
        if (request()->isPost()) {
            $param = $this->request->post();
			unset($param['file']);
            if (!empty($param['password'])) {
                //生成登录密码
                $password = setPassword(trim($param['password']));
                $param['password'] = $password['password'];
                $param['strict'] = $password['strict'];
            } else {
                unset($param['password']);
            }
            $res = $user->updateUserOne(['id' => $param['id']], $param, $this->set['db_name']);
            if ($res) {
                //添加用户组关联数据
                Db::name($this->set['auth_group_access'])->where(['uid' => $param['id']])->update(['group_id' => $param['groupid']]);

                return json(['code' => 1, 'msg' => '修改管理员成功', 'data' => url('Authgroup/index') . '?type=2']);
            } else {
                return json(['code' => 2, 'msg' => '修改管理员失败', 'data' => '']);
            }
        }
        //过滤帐号字段
        if (!empty($param['account'])) {
            unset($param['account']);
        }
        $id = input("param.id");
        //查询所有除开超级管理员之外的用户组
        $auth_group = $user->getAllAuthGroup($this->set['auth_group'], ['id' => ['<>', 1]]);
        //查询用户信息
        $data = $user->getOne('', ['id' => $id], $this->set['db_name']);
        if ($data) {
            $data['avatar_imgs'] = config('qi_niu_url') . $data['avatar_img'];
        }
        $this->assign(['auth_group' => $auth_group, 'user' => $data]);
        return $this->fetch();
    }

    /**
     * 删除用户
     */
    public function delUser() {
        $id = input('param.id');
        $user = new User();
        $res = $user->updateUserOne(['id' => $id], ['is_delete' => 1], $this->set['db_name']);
        if ($res) {
            return json(['code' => 1, 'msg' => '删除管理员成功', 'data' => '']);
        } else {
            return json(['code' => 1, 'msg' => '删除管理员失败', 'data' => '']);
        }
    }

    /**
     * 个人资料
     */
    public function PersonalInformation() {
        $user = new User();
        if (request()->isPost()) {
            $param = $this->request->post();
            //修改管理员密码
            if ($param['old_password'] && $param['new_password'] && $param['password']) {
                //查询用户信息
                $userOne = $user->getOne('', ['id' => $param['id']], $this->set['db_name']);
                //验证密码是否正确
                if (md5(md5($param['old_password']) . $userOne['strict']) != $userOne['password']) {
                    return json(['code' => 2, 'msg' => '原密码输入不正确', 'data' => '']);
                }
                //验证两次密码输入是否一致
                if ($param['new_password'] != $param['password']) {
                    return json(['code' => 2, 'msg' => '两次密码输入不一致', 'data' => '']);
                }
                //生成登录密码
                $password = setPassword(trim($param['password']));
                unset($param['old_password']);
                unset($param['new_password']);
                unset($param['password']);
                $param['password'] = $password['password'];
                $param['strict'] = $password['strict'];
            } else {
                unset($param['old_password']);
                unset($param['new_password']);
                unset($param['password']);
            }
            //图片上传
            session($this->set['db_name'] . 'portrait', $param['avatar_img']);
            $res = $user->updateUserOne(['id' => $param['id']], $param, $this->set['db_name']);
            if ($res) {
                return json(['code' => 1, 'msg' => '修改成功', 'data' => '']);
            } else {
                return json(['code' => 2, 'msg' => '修改失败', 'data' => '']);
            }
        }
        $userOne = $user->getOne('', ['id' => session($this->set['db_name'] . 'uid')], $this->set['db_name']);
        $this->assign('data', $userOne);
        return $this->fetch();
    }

}
